package com.tancire.quartz.task;

import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.core.domain.entity.SysDictData;
import com.tancire.common.utils.DateUtils;
import com.tancire.report.domain.ReportAppFlow;
import com.tancire.report.service.IReportAppFlowService;
import com.tancire.system.domain.SysApp;
import com.tancire.system.domain.SysVisitCount;
import com.tancire.system.service.ISysAppLogService;
import com.tancire.system.service.ISysAppService;
import com.tancire.system.service.ISysDictTypeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 应用相关定时任务调度
 *
 * @author chenchao
 */
@Component("appTask")
public class AppTask {

    @Resource
    private ISysAppService sysAppService;

    @Resource
    private ISysAppLogService sysAppLogService;

    @Resource
    private IReportAppFlowService reportAppFlowService;

    @Resource
    private ISysDictTypeService dictTypeService;

    /**
     * 统计流量并清空日志
     */
    public void trafficStat() {
        // 查询字典参数
        List<SysDictData> dictData = dictTypeService.selectDictDataByType("app_log_bus");
        // 查询全部应用列表
        List<SysApp> sysApps = this.sysAppService.selectSysAppList(new SysApp());
        if (ObjectUtil.isNotEmpty(sysApps)) {
            String ydDay = DateUtils.getYdDay();
            for (SysApp sysApp : sysApps) {
                List<SysVisitCount> visitCountList = this.sysAppLogService.selectYdVisitCount(sysApp.getAppId());
                // 访问数量
                if (ObjectUtil.isNotEmpty(visitCountList)) {
                    for (SysVisitCount visitCount : visitCountList) {
                        // 先查询是否有今日记录
                        ReportAppFlow queryFlow = new ReportAppFlow();
                        queryFlow.setReportDate(ydDay);
                        queryFlow.setBusKey(visitCount.getBusKey());
                        List<ReportAppFlow> reportAppFlows = this.reportAppFlowService.selectReportAppFlowList(queryFlow);
                        if (ObjectUtil.isEmpty(reportAppFlows)) {
                            ReportAppFlow reportAppFlow = new ReportAppFlow();
                            reportAppFlow.setAppId(sysApp.getAppId());
                            reportAppFlow.setAppName(sysApp.getAppName());
                            reportAppFlow.setReportDate(ydDay);
                            reportAppFlow.setVisitCount(visitCount.getCount());
                            reportAppFlow.setBusKey(visitCount.getBusKey());
                            reportAppFlow.setBusName(this.getDictLabel(visitCount.getBusKey(), dictData));
                            this.reportAppFlowService.insertReportAppFlow(reportAppFlow);
                        }
                    }
                }
            }
            // 清除昨日全部日志
            this.sysAppLogService.deleteYdLog();
        }
    }

    /**
     * 获取字典标签内容
     *
     * @param busKey
     * @param dictData
     * @return
     */
    private String getDictLabel(String busKey, List<SysDictData> dictData) {
        try {
            if (ObjectUtil.isNotEmpty(dictData)) {
                //
                List<SysDictData> collect = dictData.stream()
                        .filter(x -> x.getDictValue().equals(busKey)).collect(Collectors.toList());
                if (ObjectUtil.isNotEmpty(collect)) {
                    return collect.get(0).getDictLabel();
                }
            }
        } catch (Exception e) {
            // 异常暂时不处理
            e.printStackTrace();
        }
        return null;
    }
}
